home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Games Collection 1 / software vault.zip / software vault / CDR10 / TGE129C.ZIP / PCX2RAW / PCX.CPP next >
C/C++ Source or Header  |  1993-03-31  |  3KB  |  138 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "types.h"
  4.  
  5. #define GENERAL_ERR    0
  6. #define OK        1
  7. #define OPEN_ERR    2
  8. #define SEEK_ERR    3
  9. #define READ_ERR    4
  10. #define WRITE_ERR    5
  11. #define MEM_ERR        6
  12.  
  13.  
  14. typedef struct {
  15.   char manufacturer;
  16.   char version;
  17.   char encoding;
  18.   char bitsPerPixel;
  19.   int xmin, ymin;
  20.   int xmax, ymax;
  21.   int hres, vres;
  22.   char palette[48];
  23.   char reserved;
  24.   char colourPlanes;
  25.   int bytesPerLine;
  26.   int paletteType;
  27.   char filler[58];
  28. } PCXHEAD;
  29.  
  30. PCXHEAD header;
  31.  
  32. static int width, depth, bytes;
  33. static FILE *inFile;
  34.  
  35. int openPcxFile(char *filename, int *wide, int *deep, uchar *palette);
  36. void closePcxFile(void);
  37. int translatePcxFile(FILE *outFile, void *lineBuf);
  38. int readPcxLine(uchar *p, FILE *inFile);
  39.  
  40. int openPcxFile(char *filename, int *wide, int *deep, uchar *palette)
  41. {
  42.   // open the file
  43.   if ((inFile=fopen(filename,"rb")) == NULL)
  44.     return (OPEN_ERR);
  45.  
  46.   // read in the header
  47.   if (!fread((char *)&header, sizeof(PCXHEAD), 1, inFile))
  48.   {
  49.     fclose(inFile);
  50.     return (READ_ERR);
  51.   }
  52.  
  53.   // check to make sure it's a .PCX
  54.   if (header.manufacturer!=0x0A || header.version!=5)
  55.   {
  56.     fclose(inFile);
  57.     return (GENERAL_ERR);
  58.   }
  59.  
  60.   // find the palette
  61.   if (fseek(inFile, -769L, SEEK_END))
  62.   {
  63.     fclose(inFile);
  64.     return (SEEK_ERR);
  65.   }
  66.  
  67.   // read in the palette data
  68.   if (fgetc(inFile)!=0x0C || !fread(palette, 1, 768, inFile)==768)
  69.   {
  70.     fclose(inFile);
  71.     return (READ_ERR);
  72.   }
  73.  
  74.   // seek to start of image data
  75.   fseek(inFile, 128L, SEEK_SET);
  76.  
  77.   width = (header.xmax - header.xmin) + 1;
  78.   depth = (header.ymax - header.ymin) + 1;
  79.   bytes = header.bytesPerLine;
  80.   *wide = width;
  81.   *deep = depth;
  82.  
  83.   return (OK);
  84. }
  85.  
  86. int translatePcxFile(FILE *outFile, void *lineBuf)
  87. {
  88.   register int i, status;
  89.  
  90.   for (i=0; i<depth; i++)
  91.   {
  92.     status = readPcxLine((uchar *)lineBuf, inFile);
  93.     if (status == OK)
  94.       fwrite(lineBuf, bytes, 1, outFile);    // write the line
  95.     else
  96.       return (status);                // return error code
  97.   }
  98.  
  99.   if (!ferror(outFile))
  100.     return (OK);
  101.   else
  102.     return (WRITE_ERR);
  103. }
  104.  
  105. int readPcxLine(uchar *p, FILE *fp)
  106. {
  107.   int n=0;
  108.   register int c, i;
  109.  
  110.   // null the buffer
  111.   do
  112.   {
  113.     // get a key byte
  114.     c = fgetc(fp) & 0xFF;
  115.     // if it's a run of bytes field
  116.     if ((c&0xC0) == 0xC0)
  117.     {
  118.       // and off the high bits
  119.       i = c & 0x3F;
  120.       // get the run byte
  121.       c = fgetc(fp);
  122.       // run the byte
  123.       while (i--)
  124.         p[n++] = c;
  125.     }
  126.     // else just store it
  127.     else
  128.       p[n++] = c;
  129.   }
  130.   while (n < bytes);
  131.  
  132.   return (ferror(fp) ? GENERAL_ERR : OK);
  133. }
  134.  
  135. void closePcxFile(void)
  136. {
  137.   fclose(inFile);
  138. }